import { Component, OnInit, Input } from '@angular/core';
import { GanttService } from '../../services/gantt-chart/gantt.service';

@Component({
  selector: 'app-resize-bar',
  templateUrl: './resize-bar.component.html',
  styleUrls: ['./resize-bar.component.scss']
})
export class ResizeBarComponent implements OnInit {

  //between-hor//两个水平
  //between-ver//两个垂直
  //single-hor
  //single-ver
  //single-width
  //single-height
  @Input() public mode: string;
  @Input() public before: any;
  @Input() public after: any;
  @Input() public single: any;

  private preX = 0;
  private preY = 0;

  public constructor() { }

  public ngOnInit() {
  }

  public mousedown(event) {
    event = event || window.event;
    if (event == null) {
      return;
    }
    event.stopPropagation();
    this.preX = event.x;
    this.preY = event.y;
    document.onmousemove = (evtMove: any) => {
      evtMove = evtMove || window.event;
      if (evtMove == null) {
        return;
      }
      let moveX = evtMove.x - this.preX;
      let moveY = evtMove.y - this.preY;
      this.resizing(moveX, moveY);
      this.preX = evtMove.x;
      this.preY = evtMove.y;
    };
    document.onmouseup = (evtUp: any) => {
      document.onmousemove = null;
      document.onmouseup = null;
      this.preX = 0;
      this.preY = 0;
    };
  }

  public resizing(moveX, moveY) {
    if (this.mode == "between-hor" || this.mode == "between-ver") {
      this.resizeBoth(moveX, moveY);
    }
    if (this.mode == "single-height") {
      this.resizeSingle(moveX, moveY);
    }
  }

  public resizeBoth(moveX, moveY) {
    if (this.before == null || this.after == null) {
      return;
    }
    if (this.mode == "between-hor") {
      let beforeW = this.before.width + moveX;
      let afterW = this.after.width - moveX;
      if (beforeW > 0 && afterW > 0) {
        this.before.width = beforeW;
        this.after.width = afterW;
      }
    }
    if (this.mode == "between-ver") {
      let beforeH = this.before.height + moveY;
      let afterH = this.after.height - moveY;
      if (beforeH > 0 && afterH > 0) {
        this.before.height = beforeH;
        this.after.height = afterH;
      }
    }
  }

  public resizeSingle(moveX, moveY) {

    if (this.single == null) {
      return;
    }
    if (this.mode == "single-height") {
      let height = this.single.height - moveY;
      if (height > 0) {
        if (this.single.setHeight) {
          this.single.setHeight(height);
        }else {
          this.single.height = height;
        }
      }
    }
  }

  // between(event) {
  //   event = event || window.event;
  //   let hasError = (event == null || this.before == null || this.after == null);
  //   if (hasError) {
  //     return;
  //   }

  //   event.stopPropagation();
  //   let preX = event.x;
  //   let preY = event.y;
  //   let that = this;
  //   let beforeElem = this.before;
  //   let afterElem = this.after;
  //   document.onmousemove = (evtMove: any) => {
  //     evtMove = evtMove || window.event;
  //     let moveX = evtMove.x - preX;
  //     let moveY = evtMove.y - preY;

  //     if (that.mode == "hor") {
  //       let beforeW = beforeElem.width + moveX;
  //       let afterW = afterElem.width - moveX;
  //       if (beforeW > 0 && afterW > 0) {
  //         afterElem.width = afterW;
  //         beforeElem.width = beforeW;
  //       }
  //     } else {
  //       let beforeH = beforeElem.height + moveY;
  //       let afterH = afterElem.height - moveY;
  //       if (beforeH > 0 && afterH > 0) {
  //         beforeElem.height = beforeH;
  //         afterElem.height = afterH;
  //       }
  //     }
  //     preX = evtMove.x;
  //     preY = evtMove.y;
  //   }

  //   document.onmouseup = (evtUp: any) => {
  //     document.onmousemove = null;
  //     document.onmouseup = null;
  //   }
  // }
}
